<?php
/**
 * 管理员表模型
 * Created by PhpStorm.
 * User: qingyun
 * Date: 17/4/13
 * Time: 下午2:38
 */
namespace Common\Model;
use Think\Model;

class AdminModel extends Model{
    //  无表前缀的管理员表
    private $table;
    //  有表前缀的管理员表
    private $adminTable;
    //  表前缀
    private $prefix;

    /**
     * 构造函数初始化数据
     * AdminModel constructor.
     * @param string $adminTable
     * @param string $prefix
     */
    public function __construct($table,$prefix = ''){
        parent::__construct();
        $this -> prefix = empty($prefix) ? C('DB_PREFIX') : $prefix;
        $this -> table = $table;
        $this -> adminTable = $this -> prefix . $this -> table;
    }

    /**
     * 获取指定用户信息
     * @param $name
     * @return array
     */
    public function adminInfo($name){
        $result = $this -> query('select `id`,`name`,`salt`,`pwd`,`realName` from ' . $this -> adminTable . ' where `name` = "' . $name . '" and `status` = 1');
        if(!$result){
            return ['status' => 1, 'msg' => '操作失败', 'data' => ''];
        }
        return ['status' => 0, 'msg' => '操作成功', 'data' => $result];
    }

    /**
     * 更新登录次数
     * @param $uid
     * @return bool
     */
    public function updateLoginNum($uid){
        $result = $this -> execute('update ' . $this -> adminTable . ' set `loginNum` = `loginNum` + 1 where  `id` =' . $uid . ' and `status` = 1');
        if(!$result){
            return false;
        }
        return true;
    }

    /**
     * 更新登陆时间
     * @param $uid
     * @return bool
     */
    public function updateLoginTime($uid){
        $result = $this -> execute('update ' . $this -> adminTable . ' set `loginTime` = "' . _getDate() . '" where  `id` =' . $uid . ' and `status` = 1');
        if(!$result){
            return false;
        }
        return true;
    }
}